<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests\AdminloginRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CartController extends Controller
{
   /*
	加入购物车
    */
    public function Insert(Request $request)
    {
        // 提取数据
        $data = $request->only(['id','num','size','color']);
       // $request->session()->flush();

       // dd($data);
        //检测当前商品是否已经存在于购物车中
        if($this->check($data['id'])){
            //存在的话 修改num数量就可以
            $goods = session('cart');
           foreach($goods as $k=>$v){
                if($v['id'] == $data['id']){
                    // $v['num'] += $data['num'];
                    $goods[$k]['num'] += $data['num'];
                }
                    $goods[$k]['size'] = $data['size'];       
                    $goods[$k]['color'] = $data['color'];       
           }
           session(['cart'=>$goods]);
           // dd(session('cart'));
           // $request->session()->forget('cart');
        }else{
            //如果不存在将数据压入到cart
            //存到session
            // session(['cart'=>$data]);
            $request->session()->push('cart',$data);
           // $request->session()->forget('cart');
            
        }
        // $request->session()->forget('cart');
        echo 1;       
    }

    private function check($id)
    {
        //提取sesson里的值
        $goods = session('cart');
        //判断goods是否为空
        if(empty($goods)){
            return false;
        }
        foreach($goods as $k=>$v){
            if($v['id'] == $id){
                //返回true
            return true;
            }
        }
        //商品不存在返回false
        return false;
    }
    /*
    显示购物车信息
    */
    public function getCart()
    {
        if(empty(session('cart'))){
            //如果购物车没有物品
            $gwc = 1;
            $data = [];
            $num_zong = '';
            $total = '';
        }else{
            //如果购物车有物品
            $gwc = 0;
            //提取当前所有的购物车商品
            $goods = session('cart');
            // session()->forget('cart');
            // dd($goods);
            //遍历数据
            $data = [];
            // $dingdan_hao = 1;
            foreach($goods as $k=>$v){
                $goodss = DB::table('goods')->where('id',$v['id'])->first();
                $goodss->num = $v['num'];
                $goodss->dingdan_size = $v['size']; 
                $goodss->dingdan_color = $v['color'];
                
                $data[] = $goodss;

            }
            //购物车总数据操作
            // dd($data);
            $num_zong = count($data);
            $total = 0;
            foreach($data as $k=>$v){
                $total += $v->num*$v->price;
            }
            // dd($total);
        }
        return view('home.cart.cart',['goods'=>$data,'num_zong'=>$num_zong,'total'=>$total,'gwc'=>$gwc]);
    }

    /*
    删除购物车商品数据
    */
    public function delete(Request $request)
    {
        //获取id
        $key = $request->input('key');
        // dd($key);
        $cart = session('cart');
        unset($cart[$key]);
        // dd($cart);
        session(['cart'=>$cart]);
        echo 1;
        
    }    
    
}
